package com.gzk.oeno.ihm.common;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.gzk.oeno.business.INamed;
import com.gzk.oeno.tech.hibernate.HibernateManager;
import com.gzk.oeno.tech.ui.Formator;
import com.gzk.utils.data.IdObject;

public abstract class SimpleNameForm<K, T extends INamed & IdObject<K>> extends HibernateEditForm<K, T> {

	private JTextField nameField;
	
	protected SimpleNameForm(HibernateManager<K, T> manager){
		super(manager);
	}
	
	protected JTextField getNameField(){
		if (nameField == null){
			nameField = new JTextField(25);
			nameField.addKeyListener(autoRefreshKListener);
		}
		return nameField;
	}
	
	@Override
	protected JPanel buildForm() {
		JPanel pane = new JPanel(new GridBagLayout());
		
		GridBagConstraints gc = getNewConstraint();
		addLabelAndField(pane, Formator.boldLabel("Nom* :"), getNameField(), gc);
		
		return pane;
	}

	@Override
	protected void toForm(T object) {
		getNameField().setText(object.getName());
	}

	@Override
	protected void cleanForm() {
		getNameField().setText(null);
	}

	@Override
	protected boolean isEditable() {
		return StringUtils.isNotEmpty(getNameField().getText());
	}

}
